﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;

namespace SerialPortDal
{
    public class SendDataLogDAO
    {
        public SendDataLogDAO()
        {

        }

        public static List<SendDataLog> getAll()
        {
            DataTable tbl = SqliteHelper.ExecuteDataTable("select a.*,b.group_name,b.notes from SEND_DATA_LOG a,WORK_GROUP b WHERE a.group_no = b.group_no");
            List<SendDataLog> objList = new List<SendDataLog>();
            foreach (DataRow r in tbl.Rows)
            {
                objList.Add(parseRow2Obj(r));
            }
            return objList;
        }

        private static SendDataLog parseRow2Obj(DataRow r)
        {
            SendDataLog obj = new SendDataLog();
            obj.Create_dt = DateTime.Parse(r["create_dt"].ToString());
            obj.Send_data_content = r["send_data_content"] as string;
            obj.Send_data_log_id = Convert.ToInt32(r["send_data_log_id"]);
            obj.Send_mode = r["send_mode"] as string;
            obj.Send_name = Convert.ToString(r["send_name"]);
            obj.Send_times = Convert.ToInt32(r["send_times"]);
            obj.Status = r["status"] as string;
            obj.Group_no = Convert.ToInt32(r["group_no"]);
            obj.Group_name = r["group_name"] as string;
            WorkGroup wkObj = new WorkGroup();
            wkObj.Group_no = obj.Group_no;
            wkObj.Group_name = r["group_name"] as string;
            wkObj.Notes = r["notes"] as string;
            obj.WorkGroup = wkObj;
            return obj;
        }

        public static SendDataLog selectObjByName(string name)
        {
            SQLiteParameter[] pas = { new SQLiteParameter("send_name", name) };
            DataTable tbl = SqliteHelper.ExecuteDataTable("select a.*,b.group_name,b.notes from SEND_DATA_LOG a,WORK_GROUP b WHERE a.group_no = b.group_no and a.send_name=?", CommandType.Text, pas);
            if (tbl == null || tbl.Rows.Count == 0)
            {
                return null;
            }
            return parseRow2Obj(tbl.Rows[0]);
        }
        public static int delete(SendDataLog obj)
        {
            if (obj.Send_data_log_id > 0)
            {
                return delete(obj.Send_data_log_id);
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// 根据ID删除记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int delete(int id)
        {
            String cmd = "delete from send_data_log where send_data_log_id=?";
            SQLiteParameter[] pas = { new SQLiteParameter("send_data_log_id", id) };
            return SqliteHelper.ExecuteNonQuery(cmd, CommandType.Text, pas);
        }

        public static int save(SendDataLog obj)
        {
            String cmd = "insert into SEND_DATA_LOG(send_name,send_data_content,send_mode,status,group_no) values(?,?,?,?,?)";

            SQLiteParameter[] pas = {new SQLiteParameter("send_name",obj.Send_name),
                new SQLiteParameter("send_data_content",obj.Send_data_content),
                new SQLiteParameter("send_mode",obj.Send_mode),
                new SQLiteParameter("status",obj.Status),
                new SQLiteParameter("group_no",obj.Group_no)
                 };
            return SqliteHelper.ExecuteNonQuery(cmd, CommandType.Text, pas);
        }

        public static void update(SendDataLog obj)
        {
            String cmd = "update SEND_DATA_LOG set(send_name,send_data_content,send_mode,status,group_no) = (?,?,?,?,?) where SEND_DATA_LOG_Id=?";
            SQLiteParameter[] pas = {new SQLiteParameter("send_name",obj.Send_name),
                new SQLiteParameter("send_data_content",obj.Send_data_content),
                new SQLiteParameter("send_mode",obj.Send_mode),
                new SQLiteParameter("status",obj.Status),
                new SQLiteParameter("group_no",obj.Group_no),
                new SQLiteParameter("SEND_DATA_LOG_ID",obj.Send_data_log_id)
                 };
            SqliteHelper.ExecuteNonQuery(cmd, CommandType.Text, pas);
        }
    }
}
